package com.newland.mesdk.demo.fragment;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.newland.mesdk.demo.main.R;
import com.newland.mtype.DeviceInvokeException;
import com.newland.mtype.module.common.rfcard.RFCardType;
import com.newland.mtype.module.common.rfcard.RFKeyMode;
import com.newland.mtype.module.common.rfcard.RFResult;
import com.newland.mtype.util.Dump;
import com.newland.mtype.util.ISOUtils;
import com.newland.mesdk.demo.event.RadioGroupChangeListener;
import com.newland.mesdk.demo.interfaceimpl.RFCardInterfaceImpl;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.util.Const;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF 非接卡模块
 */
public class RFCardFragment extends Fragment implements OnClickListener {
	private MainActivity mainActivity;
	private RFCardInterfaceImpl rfCardInterfaceImpl;
	private RFCardType qpCardType = RFCardType.M1CARD;
	private Dialog nccard_dialog;
	private EditText edit_qccard_block, edit_qccard_key, edit_qccard_data, edit_snr_no;
	private RadioGroupChangeListener radioGroupChangeListener;
	private Button btnRFPowOn, btnRFTransation, btnRFStorageKey, btnRFLoadKey;
	private Button btnRFSecuLoad, btnRFSecuOut, btnRFWrite, btnRFRead;
	private Button btnRFIncrease, btnRFDecrise, btnRFPowOff;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_rfcard, null);
		mainActivity = (MainActivity) getActivity();
		initRFCardView(view);
		return view;
	}

	public void initRFCardView(View view) {
		btnRFPowOn = (Button) view.findViewById(R.id.btn_rfcard_poweron);
		btnRFPowOn.setOnClickListener(this);

		btnRFTransation = (Button) view.findViewById(R.id.btn_rfcard_tranction);
		btnRFTransation.setOnClickListener(this);

		btnRFStorageKey = (Button) view.findViewById(R.id.btn_rfcard_storage_key);
		btnRFStorageKey.setOnClickListener(this);

		btnRFLoadKey = (Button) view.findViewById(R.id.btn_rfcard_load_key);
		btnRFLoadKey.setOnClickListener(this);

		btnRFSecuLoad = (Button) view.findViewById(R.id.btn_rfcard_security_loadk);
		btnRFSecuLoad.setOnClickListener(this);

		btnRFSecuOut = (Button) view.findViewById(R.id.btn_rfcard_security_outkey);
		btnRFSecuOut.setOnClickListener(this);

		btnRFWrite = (Button) view.findViewById(R.id.btn_rfcard_write);
		btnRFWrite.setOnClickListener(this);

		btnRFRead = (Button) view.findViewById(R.id.btn_rfcard_read);
		btnRFRead.setOnClickListener(this);

		btnRFIncrease = (Button) view.findViewById(R.id.btn_rfcard_encrise);
		btnRFIncrease.setOnClickListener(this);

		btnRFDecrise = (Button) view.findViewById(R.id.btn_rfcard_decrise);
		btnRFDecrise.setOnClickListener(this);

		btnRFPowOff = (Button) view.findViewById(R.id.btn_rfcard_poweroff);
		btnRFPowOff.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (mainActivity.isDeviceInit()) {
			if (mainActivity.processingisLocked()) {
				mainActivity.showMessage("请先完成或撤销当前操作...", MessageTag.ERROR);
			} else {
				mainActivity.connectDevice();
				rfCardInterfaceImpl = new RFCardInterfaceImpl();
				switch (v.getId()) {
				case R.id.btn_rfcard_poweron:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							showRFCardPowerOnDialog();

						}
					}).start();

					break;
				case R.id.btn_rfcard_tranction:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								String str = "0084000004";
								byte req[] = ISOUtils.hex2byte(str);
								byte result[] = rfCardInterfaceImpl.call(req, 60, TimeUnit.SECONDS);
								mainActivity.showMessage("发送数据:" + str + "\r\n", MessageTag.DATA);
								mainActivity.showMessage("接收数据:" + ISOUtils.hexString(result) + "\r\n", MessageTag.DATA);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("非接卡通信异常" + e + "\r\n", MessageTag.ERROR);
								mainActivity.showMessage("非接卡是否已经上电了" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_rfcard_poweroff:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								rfCardInterfaceImpl.powerOff(60);
								mainActivity.showMessage("非接卡下电完成" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("非接卡下电异常:" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_rfcard_load_key:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							loadKeyDialog();
						}
					}).start();

					break;
				case R.id.btn_rfcard_security_loadk:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							showAuthenticaentByLoadKeyDiaolg();

						}
					}).start();

					break;
				case R.id.btn_rfcard_storage_key:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							showStorageKeyDialog();

						}
					}).start();

					break;
				case R.id.btn_rfcard_security_outkey:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							showAuthenticateByExtendKeyDialog();
						}
					}).start();

					break;
				case R.id.btn_rfcard_write:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							showRFCardWriteBlogDialog();

						}
					}).start();

					break;
				case R.id.btn_rfcard_read:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							showReadBlogDialog();

						}
					}).start();

					break;
				case R.id.btn_rfcard_encrise:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							showIncrementDialog();

						}
					}).start();

					break;
				case R.id.btn_rfcard_decrise:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							showDecrementDialog();

						}
					}).start();

					break;

				default:
					break;
				}
			}
		} else {
			mainActivity.showMessage("设备未初始化，请先初始化！" + "\r\n", MessageTag.NORMAL);
		}
	}

	// 非接卡上电对话框
	public void showRFCardPowerOnDialog() {
		mainActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				final AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mainActivity);
				builder.setTitle("非接卡类型:");
				builder.setSingleChoiceItems(new String[] { "A卡", "B卡", "M1卡" }, 2,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface arg0, int arg1) {
								switch (arg1) {
								case 0:
									qpCardType = RFCardType.ACARD;
									break;
								case 1:
									qpCardType = RFCardType.BCARD;
									break;
								case 2:
									qpCardType = RFCardType.M1CARD;
									break;
								}

							}
						});
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						try {
							new Thread(new Runnable() {

								@Override
								public void run() {
									mainActivity.showMessage("请贴非接卡！" + "\r\n", MessageTag.TIP);
									try{
										RFResult qPResult = rfCardInterfaceImpl.powerOn(qpCardType, 8);
										mainActivity.showMessage("非接卡名:" + qPResult.getQpCardType() + "\r\n",
												MessageTag.DATA);
										mainActivity.showMessage("非接卡类型:" + qPResult.getQpCardType() + "\r\n",
												MessageTag.DATA);
										if (qPResult.getCardSerialNo() == null) {
											mainActivity.showMessage("非接卡序列号:null" + "\r\n", MessageTag.DATA);
										} else {
											mainActivity.showMessage(
													"非接卡序列号:" + Dump.getHexDump(qPResult.getCardSerialNo()) + "\r\n",
													MessageTag.DATA);
										}

										if (qPResult.getATQA() == null) {
											mainActivity.showMessage("非接卡ATQA:null" + "\r\n", MessageTag.DATA);
										} else {
											mainActivity.showMessage("非接卡ATQA:" + Dump.getHexDump(qPResult.getATQA())
													+ "\r\n", MessageTag.DATA);
										}
										mainActivity.showMessage("寻卡上电完成" + "\r\n", MessageTag.NORMAL);
									}catch(Exception e){
										mainActivity.showMessage("非接卡寻卡上电异常:" + e.getMessage() + "\r\n", MessageTag.ERROR);
										mainActivity.processingUnLock();
									}
									
									
								}
							}).start();
							nccard_dialog.dismiss();

						} catch (Exception e) {
							mainActivity.showMessage("非接卡寻卡上电异常:" + e.getMessage() + "\r\n", MessageTag.ERROR);
							mainActivity.processingUnLock();
						}

					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						nccard_dialog.dismiss();
						mainActivity.processingUnLock();
					}
				});
				mainActivity.processingUnLock();
				nccard_dialog = builder.create();
				nccard_dialog.show();
				nccard_dialog.setCancelable(false);
				nccard_dialog.setCanceledOnTouchOutside(false);
			}
		});
	}

	// 加载秘钥对话框
	public void loadKeyDialog() {
		mainActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					final AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
					LayoutInflater inflater = LayoutInflater.from(mainActivity);
					final View view = inflater.inflate(R.layout.dialog_qc_key, null);
					builder.setView(view);
					LinearLayout qc_data_area = (LinearLayout) view.findViewById(R.id.qc_data_area);
					qc_data_area.setVisibility(View.GONE);
					LinearLayout qc_snr_area = (LinearLayout) view.findViewById(R.id.qc_snr_area);
					qc_snr_area.setVisibility(View.GONE);
					edit_qccard_block = (EditText) view.findViewById(R.id.edit_qccard_block);
					radioGroupChangeListener = new RadioGroupChangeListener(view, Const.DialogView.NC_CARD_KEY_DIALOG);
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							if (edit_qccard_block.getText().toString() != null) {
								try {
									RFKeyMode qpKeyMode = radioGroupChangeListener.getRfKeyMode();
									int block = Integer.valueOf(edit_qccard_block.getText().toString());
									if (block >= 0 && block < 15) {
										mainActivity.showMessage("KEY模式:" + qpKeyMode + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("密钥存储区:" + block + "\r\n", MessageTag.DATA);
										rfCardInterfaceImpl.loadKey(qpKeyMode, block);
										mainActivity.showMessage("非接卡加载密钥完成" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("输入不合法" + "\r\n", MessageTag.ERROR);
										mainActivity.processingUnLock();
									}
									nccard_dialog.dismiss();
								} catch (Exception e) {
									mainActivity.showMessage(e + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								}
							} else {
								mainActivity.showMessage("输入不为空" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							nccard_dialog.dismiss();
							mainActivity.processingUnLock();
						}
					});
					nccard_dialog = builder.create();
					nccard_dialog.setTitle("存储密钥:");
					nccard_dialog.show();
					nccard_dialog.setCancelable(false);
					nccard_dialog.setCanceledOnTouchOutside(false);
				} catch (Exception e) {
					mainActivity.showMessage("非接卡加载密钥异常:" + e + "\r\n", MessageTag.ERROR);
					mainActivity.processingUnLock();
				}

			}
		});
	}

	// 存储秘钥对话框
	public void showStorageKeyDialog() {
		mainActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {

					final AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
					LayoutInflater inflater = LayoutInflater.from(mainActivity);
					final View view = inflater.inflate(R.layout.dialog_qc_key, null);
					builder.setView(view);
					LinearLayout qc_snr_area = (LinearLayout) view.findViewById(R.id.qc_snr_area);
					qc_snr_area.setVisibility(View.GONE);
					edit_qccard_block = (EditText) view.findViewById(R.id.edit_qccard_block);
					edit_qccard_key = (EditText) view.findViewById(R.id.edit_qccard_key);
					radioGroupChangeListener = new RadioGroupChangeListener(view, Const.DialogView.NC_CARD_KEY_DIALOG);
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							if (edit_qccard_block.getText().toString() != null
									&& edit_qccard_key.getText().toString() != null) {
								try {
									RFKeyMode qpKeyMode = radioGroupChangeListener.getRfKeyMode();
									int block = Integer.valueOf(edit_qccard_block.getText().toString());
									byte key[] = edit_qccard_key.getText().toString().getBytes("GBK");
									if (block >= 0 && block < 15 && edit_qccard_key.getText().toString().length() == 6) {
										mainActivity.showMessage("KEY模式:" + qpKeyMode + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("密钥存储区:" + block + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("密钥:" + key + "\r\n", MessageTag.DATA);
										rfCardInterfaceImpl.storeKey(qpKeyMode, block, key);
										mainActivity.showMessage("非接卡存储密钥完成" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("输入不合法" + "\r\n", MessageTag.ERROR);
										mainActivity.processingUnLock();
									}
									nccard_dialog.dismiss();

								} catch (Exception e) {
									mainActivity.showMessage("非接卡存储密钥异常" + e + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								}
							} else {
								mainActivity.showMessage("输入不为空" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							nccard_dialog.dismiss();
							mainActivity.processingUnLock();
						}
					});
					nccard_dialog = builder.create();
					nccard_dialog.setTitle("存储密钥:");
					nccard_dialog.show();
					nccard_dialog.setCancelable(false);
					nccard_dialog.setCanceledOnTouchOutside(false);

				} catch (Exception e) {
					mainActivity.showMessage("非接卡存储密钥异常:" + e + "\r\n", MessageTag.ERROR);
					mainActivity.processingUnLock();
				}
			}
		});
	}

	// 写块对话框
	public void showRFCardWriteBlogDialog() {
		mainActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					final AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mainActivity);
					builder.setTitle("写块数据:");
					LayoutInflater inflater = LayoutInflater.from(mainActivity);
					final View view = inflater.inflate(R.layout.dialog_qc_data, null);
					builder.setView(view);
					edit_qccard_block = (EditText) view.findViewById(R.id.edit_qccard_block);
					edit_qccard_data = (EditText) view.findViewById(R.id.edit_qccard_data);
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							if (edit_qccard_block.getText().toString() != null
									&& edit_qccard_data.getText().toString() != null) {
								try {
									int block = Integer.valueOf(edit_qccard_block.getText().toString());
									byte input[] = (edit_qccard_data.getText().toString()).getBytes();
									if (block >= 0 && block < 255
											&& edit_qccard_data.getText().toString().length() == 16) {
										rfCardInterfaceImpl.writeDataBlock(block, input);
										mainActivity.showMessage("存储块:" + block + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("数据:" + input + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("写块数据完成" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("输入不合法" + "\r\n", MessageTag.ERROR);
										mainActivity.processingUnLock();
									}
								} catch (Exception e) {
									mainActivity.showMessage("写块数据异常:" + e.getMessage() + "\r\n", MessageTag.ERROR);
									mainActivity.showMessage("请先确定非接卡已上电" + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								}
							} else {
								mainActivity.showMessage("输入不为空" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							nccard_dialog.dismiss();
							mainActivity.processingUnLock();
						}
					});
					nccard_dialog = builder.create();
					nccard_dialog.show();
					nccard_dialog.setCancelable(false);
					nccard_dialog.setCanceledOnTouchOutside(false);
				} catch (Exception e) {
					mainActivity.showMessage("写块数据异常:" + e + "\r\n", MessageTag.ERROR);
					mainActivity.processingUnLock();
				}

			}
		});
	}

	// 读块对话框
	public void showReadBlogDialog() {
		mainActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					final AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mainActivity);
					builder.setTitle("读块数据:");
					LayoutInflater inflater = LayoutInflater.from(mainActivity);
					final View view = inflater.inflate(R.layout.dialog_qc_data, null);
					builder.setView(view);
					edit_qccard_block = (EditText) view.findViewById(R.id.edit_qccard_block);
					LinearLayout qc_data_area = (LinearLayout) view.findViewById(R.id.qc_data_area);
					qc_data_area.setVisibility(View.GONE);
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							if (edit_qccard_block.getText().toString() != null) {
								try {
									int block = Integer.valueOf(edit_qccard_block.getText().toString());
									if (block >= 0 && block < 255 && edit_qccard_data.getText().length() == 16) {
										byte output[] = rfCardInterfaceImpl.readDataBlock(block);
										mainActivity.showMessage("存储块:" + block + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("数据:" + ISOUtils.hexString(output) + "\r\n",
												MessageTag.DATA);
										mainActivity.showMessage("读块数据完成" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("输入不合法" + "\r\n", MessageTag.ERROR);
										mainActivity.processingUnLock();
									}
								} catch (Exception e) {
									mainActivity.showMessage("读块数据异常:" + e.getMessage() + "\r\n", MessageTag.ERROR);
									mainActivity.showMessage("请先确定非接卡已上电或该数据块已写入数据" + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								}

							} else {
								mainActivity.showMessage("输入不为空" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							nccard_dialog.dismiss();
							mainActivity.processingUnLock();
						}
					});
					nccard_dialog = builder.create();
					nccard_dialog.show();
					nccard_dialog.setCancelable(false);
					nccard_dialog.setCanceledOnTouchOutside(false);

				} catch (Exception e) {
					mainActivity.showMessage("读块数据异常:" + e + "\r\n", MessageTag.ERROR);
					mainActivity.processingUnLock();
				}
			}
		});
	}

	// 减量操作对话框
	public void showDecrementDialog() {
		mainActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					final AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mainActivity);
					builder.setTitle("减量数据:");
					LayoutInflater inflater = LayoutInflater.from(mainActivity);
					final View view = inflater.inflate(R.layout.dialog_qc_data, null);
					builder.setView(view);
					edit_qccard_block = (EditText) view.findViewById(R.id.edit_qccard_block);
					edit_qccard_data = (EditText) view.findViewById(R.id.edit_qccard_data);
					edit_qccard_data.setText("0000");
					TextView text_qccard_data = (TextView) view.findViewById(R.id.text_qccard_data);
					text_qccard_data.setText("数据(4位):");
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							if (edit_qccard_block.getText().toString() != null) {
								try {
									int block = Integer.valueOf(edit_qccard_block.getText().toString());
									byte[] data = (edit_qccard_data.getText().toString()).getBytes();
									if (block > 0 && block < 255 && edit_qccard_data.getText().length() == 4) {
										rfCardInterfaceImpl.decrementOperation(block, data);
										mainActivity.showMessage("存储块:" + block + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("数据:" + ISOUtils.dumpString(data) + "\r\n",
												MessageTag.DATA);
										mainActivity.showMessage("减量操作完成" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("输入不合法" + "\r\n", MessageTag.ERROR);
										mainActivity.processingUnLock();
									}
								} catch (Exception e) {
									mainActivity.showMessage("减量操作异常:" + e.getMessage() + "\r\n", MessageTag.ERROR);
									mainActivity.showMessage("请先确定非接卡已上电" + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								}

							} else {
								mainActivity.showMessage("输入不为空" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							nccard_dialog.dismiss();
							mainActivity.processingUnLock();
						}
					});
					nccard_dialog = builder.create();
					nccard_dialog.show();
					nccard_dialog.setCancelable(false);
					nccard_dialog.setCanceledOnTouchOutside(false);

				} catch (Exception e) {
					mainActivity.showMessage("异常:" + e + "\r\n", MessageTag.ERROR);
					mainActivity.processingUnLock();
				}
			}
		});
	}

	// 增量操作对话框
	public void showIncrementDialog() {
		mainActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					final AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mainActivity);
					builder.setTitle("增量数据:");
					LayoutInflater inflater = LayoutInflater.from(mainActivity);
					final View view = inflater.inflate(R.layout.dialog_qc_data, null);
					builder.setView(view);
					edit_qccard_block = (EditText) view.findViewById(R.id.edit_qccard_block);
					edit_qccard_data = (EditText) view.findViewById(R.id.edit_qccard_data);
					edit_qccard_data.setText("0000");
					TextView text_qccard_data = (TextView) view.findViewById(R.id.text_qccard_data);
					text_qccard_data.setText("数据(4位):");
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							if (edit_qccard_block.getText().toString() != null) {
								try {
									int block = Integer.valueOf(edit_qccard_block.getText().toString());
									byte[] data = (edit_qccard_data.getText().toString()).getBytes();
									if (block > 0 && block < 255 && edit_qccard_data.getText().length() == 4) {
										rfCardInterfaceImpl.incrementOperation(block, data);
										mainActivity.showMessage("存储块:" + block + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("数据:" + ISOUtils.dumpString(data) + "\r\n",
												MessageTag.DATA);
										mainActivity.showMessage("增量操作完成" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("输入不合法" + "\r\n", MessageTag.ERROR);
										mainActivity.processingUnLock();
									}
								} catch (Exception e) {
									mainActivity.showMessage("增量操作异常:" + e.getMessage() + "\r\n", MessageTag.ERROR);
									mainActivity.showMessage("请先确定非接卡已上电" + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								}
							} else {
								mainActivity.showMessage("输入不为空" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							nccard_dialog.dismiss();
							mainActivity.processingUnLock();
						}
					});
					nccard_dialog = builder.create();
					nccard_dialog.show();
					nccard_dialog.setCancelable(false);
					nccard_dialog.setCanceledOnTouchOutside(false);

				} catch (Exception e) {
					mainActivity.showMessage("增量操作异常:" + e + "\r\n", MessageTag.ERROR);
					mainActivity.processingUnLock();
				}
			}
		});
	}

	// 使用外部的密钥进行认证对话框
	public void showAuthenticateByExtendKeyDialog() {
		mainActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					final AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
					LayoutInflater inflater = LayoutInflater.from(mainActivity);
					final View view = inflater.inflate(R.layout.dialog_qc_key, null);
					builder.setView(view);
					TextView text_key = (TextView) view.findViewById(R.id.text_key);
					text_key.setText("外部密钥(6位):");
					TextView text_block = (TextView) view.findViewById(R.id.text_block);
					text_block.setText("认证的块号(0-255):");
					edit_snr_no = (EditText) view.findViewById(R.id.edit_snr_no);
					edit_qccard_block = (EditText) view.findViewById(R.id.edit_qccard_block);
					edit_qccard_key = (EditText) view.findViewById(R.id.edit_qccard_key);
					radioGroupChangeListener = new RadioGroupChangeListener(view, Const.DialogView.NC_CARD_KEY_DIALOG);
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							if (edit_qccard_block.getText().toString() != null
									&& edit_qccard_key.getText().toString() != null
									&& edit_snr_no.getText().toString() != null) {
								try {
									RFKeyMode qpKeyMode = radioGroupChangeListener.getRfKeyMode();
									int block = Integer.valueOf(edit_qccard_block.getText().toString());
									byte snr[] = edit_snr_no.getText().toString().getBytes("GBK");
									byte key[] = edit_qccard_key.getText().toString().getBytes("GBK");
									if (block >= 0 && block < 255 && edit_qccard_key.getText().toString().length() == 6
											&& edit_snr_no.getText().toString().length() == 4) {
										mainActivity.showMessage("KEY模式:" + qpKeyMode + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("SNR序列号:" + snr + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("认证块号:" + block + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("外部密钥:" + key + "\r\n", MessageTag.DATA);
										rfCardInterfaceImpl.authenticateByExtendKey(qpKeyMode, snr, block, key);
										mainActivity.showMessage("非接卡使用外部密钥认证完成" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("输入不合法" + "\r\n", MessageTag.ERROR);
										mainActivity.processingUnLock();
									}
									nccard_dialog.dismiss();

								} catch (DeviceInvokeException e) {
									mainActivity.showMessage(e.getMessage() + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								} catch (UnsupportedEncodingException e) {
									mainActivity.showMessage("非接卡外部密钥认证异常" + "UnsupportedEncodingException" + "\r\n",
											MessageTag.ERROR);
									mainActivity.processingUnLock();
								} catch (Exception e) {
									mainActivity.showMessage("非接卡外部密钥认证异常" + e + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								}
							} else {
								mainActivity.showMessage("输入不为空" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							nccard_dialog.dismiss();
							mainActivity.processingUnLock();
						}
					});
					nccard_dialog = builder.create();
					nccard_dialog.setTitle("外部密钥认证:");
					nccard_dialog.show();
					nccard_dialog.setCancelable(false);
					nccard_dialog.setCanceledOnTouchOutside(false);
				} catch (Exception e) {
					mainActivity.showMessage("非接卡外部密钥认证异常:" + e + "\r\n", MessageTag.ERROR);
					mainActivity.processingUnLock();
				}
			}
		});
	}

	// 使用加载的密钥进行认证对话框
	public void showAuthenticaentByLoadKeyDiaolg() {
		mainActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					final AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
					LayoutInflater inflater = LayoutInflater.from(mainActivity);
					final View view = inflater.inflate(R.layout.dialog_qc_key, null);
					builder.setView(view);
					LinearLayout qc_data_area = (LinearLayout) view.findViewById(R.id.qc_data_area);
					qc_data_area.setVisibility(View.GONE);
					TextView text_block = (TextView) view.findViewById(R.id.text_block);
					text_block.setText("认证的块号(0-255):");
					edit_snr_no = (EditText) view.findViewById(R.id.edit_snr_no);
					edit_qccard_block = (EditText) view.findViewById(R.id.edit_qccard_block);
					radioGroupChangeListener = new RadioGroupChangeListener(view, Const.DialogView.NC_CARD_KEY_DIALOG);
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							if (edit_qccard_block.getText().toString() != null
									&& edit_snr_no.getText().toString() != null) {
								try {
									RFKeyMode qpKeyMode = radioGroupChangeListener.getRfKeyMode();
									int block = Integer.valueOf(edit_qccard_block.getText().toString());
									byte snr[] = edit_snr_no.getText().toString().getBytes("GBK");
									if (block >= 0 && block < 255 && edit_snr_no.getText().toString().length() == 4) {
										mainActivity.showMessage("KEY模式:" + qpKeyMode + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("SNR序列号:" + snr + "\r\n", MessageTag.DATA);
										mainActivity.showMessage("认证块号:" + block + "\r\n", MessageTag.DATA);
										rfCardInterfaceImpl.authenticateByLoadedKey(qpKeyMode, snr, block);
										mainActivity.showMessage("非接卡使用加载密钥认证完成" + "\r\n", MessageTag.NORMAL);
										mainActivity.processingUnLock();
									} else {
										mainActivity.showMessage("输入不合法" + "\r\n", MessageTag.ERROR);
										mainActivity.processingUnLock();
									}
									nccard_dialog.dismiss();

								} catch (DeviceInvokeException e) {
									mainActivity.showMessage("使用加载密钥认证异常：" + e.getMessage() + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								} catch (UnsupportedEncodingException e) {
									mainActivity.showMessage("非接卡使用加载密钥认证异常" + e.getMessage() + "\r\n",
											MessageTag.ERROR);
									mainActivity.processingUnLock();
								} catch (Exception e) {
									mainActivity.showMessage("使用加载密钥认证异常：" + e.getMessage() + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								}
							} else {
								mainActivity.showMessage("输入不为空" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					});
					builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							nccard_dialog.dismiss();
							mainActivity.processingUnLock();
						}
					});
					nccard_dialog = builder.create();
					nccard_dialog.setTitle("加载密钥认证:");
					nccard_dialog.show();
					nccard_dialog.setCancelable(false);
					nccard_dialog.setCanceledOnTouchOutside(false);
				} catch (Exception e) {
					mainActivity.showMessage("非接卡使用加载密钥认证异常:" + e + "\r\n", MessageTag.ERROR);
					mainActivity.processingUnLock();
				}
			}
		});
	}
}
